﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SP2010PreRequisites_Installer
{
    class Helper
    {
        public const char SEPERATOR_ITEM = '#';
        public const string FILE_EXISTS = "File already downloaded. Skipping the download.";

        public string LastException { get; set; } = string.Empty;

        public PreRequisite[] GetPreRequisitesList()
        {
            List<PreRequisite> preRequisites = new List<PreRequisite>();

            string[] allPreRequisites = ReadPreRequisitesFromFile();

            if (allPreRequisites.Length > 0)
            {
                foreach (string item in allPreRequisites)
                {
                    string[] preRequisiteComponents = item.Split(SEPERATOR_ITEM);

                    if (preRequisiteComponents.Length > 0)
                    {
                        Uri preRequisitePath = new Uri(preRequisiteComponents[0].Replace ("\"", string.Empty));
                        string preRequisiteFileName = preRequisitePath.Segments[preRequisitePath.Segments.Length - 1];

                        preRequisites.Add(new PreRequisite { Title = preRequisiteComponents[1].Trim(),
                            Path = preRequisiteComponents[0].Replace ("\"", string.Empty).Trim(),
                            FileName = preRequisiteFileName
                        });
                    }
                }
            }

            return preRequisites.ToArray();
        }

        private string[] ReadPreRequisitesFromFile()
        {
            string preRequisiteFilePath = Path.Combine(Directory.GetCurrentDirectory(), "Data", "SP2010PreRequisitesList.txt");
            return File.ReadAllLines(preRequisiteFilePath);
        }

        public string DownloadFile (string PreRequisitePath, string PreRequisiteFileName)
        {

            if (File.Exists(PreRequisiteFileName))
            {
                LastException = FILE_EXISTS;

                return LastException;
            }

            try
            {
                WebClient wb = new WebClient();
                wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36");
                wb.DownloadFile(PreRequisitePath, PreRequisiteFileName);
            }
            catch (Exception ex)
            {
                LastException = ex.Message;
                return ex.Message;
            }

            return string.Empty;
        }

        public void Install(string InstallApp, string InstallArgs)
        {
            Process installProcess = new Process();
            //settings up parameters for the install process
            installProcess.StartInfo.FileName = InstallApp;
            installProcess.StartInfo.Arguments = InstallArgs;

            installProcess.Start();

            installProcess.WaitForExit();
            // Check for sucessful completion
            LastException = (installProcess.ExitCode == 0) ? string.Empty : "Error occurred while installing ";
        }
    }

    public class PreRequisite
    {
        public string Title;
        public string Path;
        public string FileName;
        public bool FailedToDownload;
        public bool FailedToInstall;
        public string LocalFilePath;        
    }
}
